#include "main.h"
#include <stm32f0xx.h>
/*variables---------------------*/
uint32_t x;
uint32_t y;
volatile uint16_t State_state=0x15;
/* Set DAC value, DAC_OUT1 = (3.3 * DAC_value) / 4095 and 	*/
uint16_t DAC_value = 800;
//double reference = 0.4;

uint32_t Frequency=200000;
uint32_t Frequency_max=1200000;
uint32_t Frequency_min=1000000;
volatile uint16_t adc_reading;
//Phase_address: 
volatile uint16_t Phase_state=0x15;

int a;

int main(void)
{
	// Initialize PWM output pins
	STM_EVAL_LEDInit(LED3);	
	// DAC
	DAC_config();
	//DAC_value= (uint16_t) reference*4095/3.3;
	DAC_SetChannel1Data(DAC_Align_12b_R, DAC_value);
	
		// PWM DAC
	//PWM_DAC_start();
	
	Phase_init();
 //COMPARATOR
	//comp_init();

	Phase_state=0x15;
  //GPIOB->ODR = 0x15;
	PHASE_TIM15_start();
	
	//Frequency=Frequency_max;
	//FREQUENCY_TIM2_start();
//	STM_EVAL_LEDOn(LED3);
	// ADC
	//adc_init();
	//ADC_StartOfConversion(ADC1);
	
	//PHI_init(PHI3);
	//GPIOB->ODR = 0x2a;
	
	//#define hysteresis
	#ifndef hysteresis
	while(1)
	{
	}
	#endif
	
  for (a=0;a<20000000;a++)
		{
		}
	TIM_Cmd(TIM15, DISABLE);	
  GPIOB->ODR = 0x00;
	comp_init();
	STM_EVAL_LEDOn(LED3);
	while(1)
	{
	}


	//STM_EVAL_LEDToggle(LED3);		
	//	return(0);	
}


void change_Frequency()
{
	uint16_t TimerPeriod;
	if (Frequency<Frequency_min)
		Frequency=Frequency_max;
	TimerPeriod = (SystemCoreClock / Frequency ) - 1;
	Frequency=Frequency-20000;
	TIM15->ARR =TimerPeriod;
}